package util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author name:dss
 * @version 1.0
 * @description: TODO
 * @date 2022/8/242022/8/24
 */
public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory;



    //静态代码块，一初始就加载，只加载一次
    static {
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }


    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
